package review.data.action;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import review.config.Constants;
import review.data.service.ScienceService;
import review.entity.Science;

import java.util.List;

@Controller
@RequestMapping("/syatem")
@Api(tags = { "专业操作接口" })
public class ScienceAction {
  @Autowired
  private ScienceService scienceService;

  @ApiOperation(value = "添加专业", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/add_Science", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object add(Science science) {
    JSONObject obj = new JSONObject();
    try {
      scienceService.addScience(science);
      obj.put(Constants.SUCCESS, true);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "删除专业", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/delete_Science", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object delete(Science science) {
    JSONObject obj = new JSONObject();
    try {
      scienceService.deleteScience(science);
      obj.put(Constants.SUCCESS, true);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "修改专业", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/update_Science", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object update(Science science) {
    JSONObject obj = new JSONObject();
    try {
      scienceService.updateScience(science);
      obj.put(Constants.SUCCESS, true);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "查询全部专业", notes = "", httpMethod = "GET")
  @RequestMapping(value = "/select_sciences", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object getMenu() {

    List<Science> sciences = scienceService.findAll();
    return scienceService.transformJSON(sciences);
  }

  @ApiOperation(value = "分页查询专业", notes = "", httpMethod = "GET")
  @RequestMapping(value = "/query_Science", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object query(Science sc, @RequestParam(defaultValue = "1") int page,
      @RequestParam(defaultValue = "10") int size) {
    JSONObject obj = new JSONObject();
    try {
      Page<Science> sciences = scienceService.findAll(page, size, sc);
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, sciences);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

}
